<?php
namespace app\api\controller;

use app\common\Tools as CommonTools;
use app\model\Apply;
use app\model\Cassette;
use app\model\Merchant as ModelMerchant;
use app\model\MerchantCate;
use app\model\OrderMembers;
use app\model\Orders;
use app\Request;
use app\services\RedisService;
use think\db\Query;
use think\facade\Db;

//
class Merchant extends ApiBase
{

    //商家分类
    public function cate_list(){
        $list = MerchantCate::where('status',0)->order('id asc')->select();
        return app('json')->success('ok',$list);
    }

    //商家列表
    public function merchant_list(Request $request)
    {
        $search = $request->getMore([
            ['page', 1],
            ['limit', 10],
            ['cate_id', 0],
        ]);
        $where = [];
        $where[] = ['status','=',1];
        if($search['cate_id'] > 0){
            $where[] = ['cate_id','=',$search['cate_id']];
        }
       $list = ModelMerchant::with(['cate','cassette'])->where($where)->order('sort desc')->page($search['page'],$search['limit'])->append(['is_open'])->select();
       return app('json')->success('ok',$list);
    }

    //商家详情
    public function merchant_detail(Request $request)
    {
        $post_data = $request->getMore([
            ['id', 0]
        ]);
        if(!$post_data['id'] > 0) return app('json')->fail('Id不得为空');
        if(!$detail = ModelMerchant::with(['cate','cassette','order'=>function(Query $query){
            $query->where('status',1)->where('start_timestamp','>',time())->with('cassette')->append(['timer']);
        }])->withCount(['order'=>function(Query $query){
            $query->where('status',1);
        }])->where('id',$post_data['id'])->where('status',1)->append(['is_open'])->find())  return app('json')->fail('商家不存在');
        $detail->slides = explode('|',$detail->slides);
        $detail->join_users = $detail->members;
        return app('json')->success('ok',$detail);
    }


    //商户入驻申请
    public function apply(Request $request){
        $post_data = $request->postMore([
            ['title', ''],
            ['province', ''],
            ['city', ''],
            ['area', ''],
            ['address', ''],
            ['user_name', ''],
            ['user_phone', ''],
            ['merchant_type', ''],
        ]);
        CommonTools::check($post_data, [
            'title' => 'require',
            'province' => 'require',
            'city' => 'require',
            'area' => 'require',
            'address' => 'require',
            'user_name' => 'require',
            'user_phone' => 'require|mobile',
            'merchant_type' => 'require',
        ]);
        $user_info = $this->get_user_info();
        $post_data['uid'] = $user_info['id'];
        if(ModelMerchant::where('mobile',$post_data['user_phone'])->find())  return  app('json')->fail('该手机号商户已存在');
        if($apply = Apply::where('user_phone',$post_data['user_phone'])->find()){
            if($apply->status == 0){
                return  app('json')->fail('正在审核中');
            }
            if($apply->status == 1){
                return  app('json')->fail('该商户已提交，请勿重复申请');
            }
            $apply->status = 0;
            $apply->save();
        }else{
            Apply::create($post_data);
        }
        return  app('json')->success('提交失败');
    }
}